/*

Copyright (c) 2008, Dependable Systems Lab, EPFL
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.
    * Neither the name of the Dependable Systems Lab, EPFL nor the names of its 
      contributors may be used to endorse or promote products derived from this 
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

package ch.epfl.dslab.conferrng.plugins.errorgenerators;

public class KeyboardLayout {

	/** this array represents a keyboard layout */
	public static String keyboardLayout[][];
		
	/** no modifier is pressed */
	public static final int MODIFIER_NONE = 0;
	/** shift modifier is pressed*/
	public static final int MODIFIER_SHIFT = 1;
	/** alt gr modifier is pressed*/
	public static final int MODIFIER_ALT = 2;
		
	static {
		keyboardLayout = new String[3][4];
		
		keyboardLayout[MODIFIER_NONE][0] = "§1234567890'^";
		keyboardLayout[MODIFIER_NONE][1] = "qwertzuiopüü";
		keyboardLayout[MODIFIER_NONE][2] = "asdfghjklöä$";
		keyboardLayout[MODIFIER_NONE][3] = "<yxcvbnm,.-";
	
		keyboardLayout[MODIFIER_SHIFT][0] = "°+\"*ç%&/()=?`";
		keyboardLayout[MODIFIER_SHIFT][1] = "QWERTZUIOPè!";
		keyboardLayout[MODIFIER_SHIFT][2] = "ASDFGHJKLéà£";
		keyboardLayout[MODIFIER_SHIFT][3] = ">YXCVBNM;:_";
		
		keyboardLayout[MODIFIER_ALT][0] = "¬|@#¼½¬¦¢]}'~";
		keyboardLayout[MODIFIER_ALT][1] = "@ł€¶ŧ←↓→øþ[]";
		keyboardLayout[MODIFIER_ALT][2] = "æßðđŋħjĸł'{}";
		keyboardLayout[MODIFIER_ALT][3] = "\\«»¢“”nµ···";
	}
	
	/**
	 * This class represent the key that has to be pressed on the keyboard to generate
	 * a given character
	 *
	 */
	public static class KeyPos {
		int modifier;
		int line;
		int col;
		public KeyPos(int modifier, int line, int col) {
			super();
			this.modifier = modifier;
			this.line = line;
			this.col = col;
		}
		
		boolean isNotFound() {
			return modifier == -1 && line == -1 && col == -1;
		}
	}

	/**
	 * Find the key and modifier that generate a given character
	 * @param c the character that has to be generated
	 * @return the corresponding key
	 */
	public static KeyPos findCharacter( char c) {
		int x = -1 ,y = -1, z = -1;
		
		outer: for (int i = 0 ; i < keyboardLayout.length; i++) {
			for (int j = 0 ; j < keyboardLayout[i].length; j++) {
				if (keyboardLayout[i][j].indexOf(c) != -1 ) {
					x = i;
					y = j;
					z = keyboardLayout[i][j].indexOf(c);
					break outer;
				}
			}
		}
		
		return new KeyPos(x,y,z); 
	}
	
	/**
	 * Get all characters that can be generated by a key near the key
	 * that generates a given character. The modifier is assumed to be 
	 * the same
	 * 
	 * @param c the character that is generated by the key which neighbors
	 * 			have to be returned   
	 * @return a string containing all the neighbors of the key
	 */
	public static String getNeighbors(char c) {

		KeyPos p = findCharacter(c);				
		
		if (p.isNotFound()) return null;
		
		String ret = "";
		
		if (p.line > 0 ) {
			ret += keyboardLayout[p.modifier][p.line - 1 ].charAt(p.col);
		}		
		if (p.line < 3) {
			ret += keyboardLayout[p.modifier][p.line + 1 ].charAt(p.col);
		}		
		if (p.col > 0) {
			ret += keyboardLayout[p.modifier][p.line].charAt(p.col - 1);			
		}		
		if (p.col < keyboardLayout[p.modifier][p.line].length() - 1) {
			ret += keyboardLayout[p.modifier][p.line].charAt(p.col + 1);
		}		
		return ret;
		
	}
	
}
